﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using Abp.Runtime.Caching;
using AutoMapper.QueryableExtensions;
using FwProject.Core.Restaurant;
using FwProject.Core.TypeNumber;
using FwProject.Manager;
using FwProject.Restaurant.Dto;
using X.PagedList;

namespace FwProject.Restaurant
{
    [AbpAuthorize]
    public class LoadingCarAppService : FwProjectAppServiceBase, ILoadingCarAppService
    {
        public readonly IRepository<LoadingCarInfo, Guid> _repositoryLoadingCarInfo;
        public readonly IRepository<LoadCar, Guid> _repositoryLoadCar;
        public readonly IRepository<TypeNumberInfo, Guid> _repositoryTypeNumberInfo;

        private readonly LoadingCarManager _loadingcarManager;
        public readonly ICacheManager _catchmanager;
        public IGuidGenerator _guid { get; set; }
        public IObjectMapper _map { get; set; }
        public LoadingCarAppService(
            ICacheManager catchmanager, IRepository<LoadCar, Guid> repositoryLoadCar,
            IRepository<LoadingCarInfo, Guid> repositoryLoadingCarInfo,
            IRepository<TypeNumberInfo, Guid> repositoryTypeNumberInfo, LoadingCarManager loadingcarManager)
        {
            _catchmanager = catchmanager;
            _repositoryLoadingCarInfo = repositoryLoadingCarInfo;
            _repositoryTypeNumberInfo = repositoryTypeNumberInfo;
            _loadingcarManager = loadingcarManager;
            _guid = SequentialGuidGenerator.Instance;
        }

        /// <summary>
        /// 添加一条装车明细信息
        /// </summary>
        /// <returns>The create.</returns>
        /// <param name="input">Input.</param>
        public async Task<Guid> Create(LoadingCarInputDto input)
        {
            var typeNumber = await _repositoryTypeNumberInfo.GetAsync(input.TypeNumberInfoId);
            var model = _map.Map<LoadingCarInfo>(input);
            model.Id = _guid.Create();
            model.TypeNumberName = typeNumber.Name;
            return await _loadingcarManager.CreateLoadingcar(model);
        }

        /// <summary>
        /// 获取一条装车明细信息
        /// </summary>
        /// <returns>The by identifier async.</returns>
        /// <param name="id">Identifier.</param>
        public async Task<LoadingCarDto> GetByIdAsync(Guid id)
        {
            var model = await _repositoryLoadingCarInfo.GetAsync(id);
            return _map.Map<LoadingCarDto>(model);

        }
        /// <summary>
        /// 获取装车明细信息列表
        /// </summary>
        /// <returns>The restaurant paged list async.</returns>
        /// <param name="装车明细编号">Page index.</param>
        public async Task<IPagedList<LoadingCarDto>> GetPagedListAsync(int pageIdx, int pageSize, Guid loadcarId, DateTime? begindateTime, DateTime? enddatetime)
        {
            var listmodel = (await GetCache).AsQueryable();
            if (begindateTime != null)
                listmodel.Where(o => o.CreationTime >= begindateTime);

            if (enddatetime != null)
                listmodel.Where(o => o.CreationTime <= enddatetime);
            if (loadcarId != Guid.Empty)
                listmodel.Where(o => o.LoadCarId == loadcarId);

            return await listmodel.ProjectTo<LoadingCarDto>().ToPagedListAsync(pageIdx, pageSize);
        }
        /// <summary>
        /// 修改装车明细信息
        /// </summary>
        /// <returns>The update.</returns>
        /// <param name="id">Identifier.</param>
        /// <param name="input">Input.</param>
        public async Task Update(Guid id, LoadingCarInputDto input)
        {
            var typeNumber = await _repositoryTypeNumberInfo.GetAsync(input.TypeNumberInfoId);
            var model = await _repositoryLoadingCarInfo.GetAsync(id);
            model = _map.Map(input, model);
            model.TypeNumberName = typeNumber.Name;
            await _loadingcarManager.UpdateLoadingcar(model);

        }

        public async Task<List<LoadingCarDto>> GetAllListByloadId(Guid id)
        {
            //var query= (await GetCache).AsQueryable().Where(o=>o.LoadCarId==id);
            var query =await _repositoryLoadingCarInfo.GetAllIncluding(o => o.TypeNumberInfoModel, o => o.LoadCarModel).Where(o => o.LoadCarId == id).ToListAsync();
            return _map.Map<List<LoadingCarDto>>(query);
        }

        private Task<List<LoadingCarInfo>> GetCache
        {
            get
            {
                ICache cache = _catchmanager.GetCache(nameof(LoadingCarInfo));
                return cache.GetAsync("list", async () => await _repositoryLoadingCarInfo.GetAllIncluding(o => o.TypeNumberInfoModel, o => o.LoadCarModel).ToListAsync());
            }

        }
    }
}
